﻿Public Class MonsterDataDesigner

    Private _typeSource As DataTable
    Private _monsterDS As MonsterDS

    Public Sub New()

        InitializeComponent()

        _typeSource = New DataTable
        _typeSource.Columns.Add(AppConstants.ValueColumnName, GetType(Short))
        _typeSource.Columns.Add(AppConstants.DisplayColumnName, GetType(String))

        For Each monster As MonsterObj.eMonsterType In [Enum].GetValues(GetType(MonsterObj.eMonsterType))

            _typeSource.Rows.Add(New Object() {CShort(monster), AppConstants.TranslateMonsterTypeName(monster)})

        Next

        'Add columns from MonsterDS
        LoadCsv()
        AddMonsterColumns()
        
    End Sub


#Region " Event Handlers "

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim writer As New CsvWriter(PrepMonsterDSForWriter(_MonsterDS), PrepMonsterDSHeadersForWriter(_MonsterDS))
        writer.Write(AppConstants.MonsterDesignerCsvPath)

    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        LoadCsv()
        ForceComboBoxUpdates()
    End Sub

    Private Sub DgrMonsters_CellMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles dgrMonsters.CellMouseClick
        If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 Then
            dgrMonsters.BeginEdit(False)
        End If
    End Sub

    Private Sub Form_Shown() Handles Me.Shown
        ForceComboBoxUpdates()
    End Sub

#End Region


#Region " Private Methods "

    Private Sub LoadCsv()

        Dim reader As New CsvReader(AppConstants.MonsterDesignerCsvPath)

        _monsterDS = GetMonsterDS(reader.Data)
        eMonsterTypeBindingSource.SuspendBinding()
        eMonsterTypeBindingSource.DataSource = _monsterDS
        eMonsterTypeBindingSource.DataMember = "Monster"

        ' todo: Recreate dgrMonsters, either programatically, or with column names that match MonsterDS - Matt

    End Sub

    Private Sub AddMonsterColumns()

        Dim txtCell As DataGridViewTextBoxCell = New DataGridViewTextBoxCell
        Dim chkCell As DataGridViewCheckBoxCell = New bapDataGridViewCheckBoxCell

        For Each colMonster As DataColumn In _monsterDS.Tables("Monster").Columns

            Dim dgrCol As New DataGridViewColumn
            With dgrCol

                If colMonster.DataType = GetType(Boolean) Then
                    .CellTemplate = chkCell

                ElseIf colMonster.ColumnName = "Type" Then

                    Dim cboCol As New DataGridViewComboBoxColumn
                    cboCol.ValueMember = AppConstants.ValueColumnName
                    cboCol.DisplayMember = AppConstants.DisplayColumnName
                    cboCol.DataSource = _typeSource
                    dgrCol = cboCol

                Else

                    .CellTemplate = txtCell

                End If
                .Name = colMonster.ColumnName
                .HeaderText = colMonster.Caption
                .ValueType = colMonster.DataType
                .DataPropertyName = colMonster.ColumnName
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

            End With

            dgrMonsters.Columns.Add(dgrCol)

        Next

        eMonsterTypeBindingSource.ResumeBinding()

    End Sub

    ''' <summary>
    ''' return monster datatable
    ''' based on data from file
    ''' </summary>
    ''' <param name="csvData"></param>
    ''' <returns></returns>
    ''' <remarks>2011/02/23 - mdm - minor adjustments</remarks>
    Private Function GetMonsterDS(ByVal csvData As List(Of List(Of String))) As MonsterDS

        Dim monsterSet As New MonsterDS()
        Dim monsterTable As MonsterDS.MonsterDataTable = monsterSet.Tables("Monster")

        For Each csvRow As List(Of String) In csvData

            Dim monsterRow As MonsterDS.MonsterRow = monsterTable.NewRow()

            For i As Integer = 0 To monsterTable.Columns.Count - 1

                If monsterTable.Columns(i).DataType = GetType(Int16) Then
                    monsterRow(i) = CShort(csvRow(i))
                ElseIf monsterTable.Columns(i).DataType = GetType(Boolean) Then
                    monsterRow(i) = CBool(csvRow(i))
                Else
                    monsterRow(i) = csvRow(i)
                End If
            Next

            monsterTable.Rows.Add(monsterRow)
        Next

        Return monsterSet

    End Function

    ''' <summary>
    ''' make all cbos on the grid
    ''' load their value to their
    ''' editing control
    ''' </summary>
    ''' <remarks>added 2011/02/23 - mdm
    ''' not really working... nevermind</remarks>
    Private Sub ForceComboBoxUpdates()

        Dim tableMonsters As MonsterDS.MonsterDataTable = _monsterDS.Tables("Monster")

        For Each col As DataGridViewColumn In dgrMonsters.Columns

            Dim cboCol As DataGridViewComboBoxColumn = Nothing
            cboCol = TryCast(col, DataGridViewComboBoxColumn)

            If cboCol IsNot Nothing Then

                'this column was successfully cast as a combobox column,
                'let's force an update to it's editing control
                For Each row As DataGridViewRow In dgrMonsters.Rows

                    If tableMonsters.Rows.Count > row.Index Then
                        row.Cells(col.Index).Value = CShort(tableMonsters.Rows(row.Index).Item(col.Index))
                    Else
                        row.Cells(col.Index).Value = CShort(MonsterObj.eMonsterType.None)
                    End If

                Next

            End If

        Next

    End Sub

    Private Function PrepMonsterDSForWriter(ByVal MonsterSet As MonsterDS) As List(Of List(Of String))

        Dim MonsterData As New List(Of List(Of String))

        For Each dr As MonsterDS.MonsterRow In MonsterSet.Tables("Monster").Rows
            Dim rowList As New List(Of String)
            For Each dcol As DataColumn In MonsterSet.Tables("Monster").Columns
                rowList.Add(dr(dcol.ColumnName).ToString())
            Next
            MonsterData.Add(rowList)
        Next

        Return MonsterData

    End Function

    Private Function PrepMonsterDSHeadersForWriter(ByVal MonsterSet As MonsterDS) As List(Of String)

        Dim headers As New List(Of String)

        For Each dcol As DataColumn In MonsterSet.Tables("Monster").Columns
            headers.Add(dcol.Caption)
        Next

        Return headers

    End Function

#End Region


End Class